package app.agent.tests

import app.comet.{Subscriber, ViewTest}
import im.mange.jetboot.Html._
import im.mange.jetboot.bootstrap3.Bootstrap
import im.mange.jetboot.js.JsCmdFactory
import im.mange.jetboot.widget.{ButtonPresentation, ServerSideButton}
import im.mange.jetboot._
import im.mange.shoreditch.engine.model.Test
import Css._
import JsCmdFactory._
import Bootstrap._

//TODO: this needs a good old tidy up
//TODO: ultimately I probably need to be a table, so layout is nice
//TODO: only show add when asked and click on a row to show/edit the details
//TODO: obviously these would evenutally show buttons for each test e.g. run etc
//TODO: the subscriber should not be in here .. that's definitely an agent concern not presentation
case class TestsContentPresentation(tests: List[Test], subscriber: Subscriber) extends Renderable {
  def render = Composite(
//      Composite(tests.map(s => Composite(div(id = "label" + s.id, span(Label("T" + s.id, labelDefault), span(s.name).styles(marginLeft("5px"))).styles(fontSize(smaller)), viewButton(s.id)).styles(clear(both)))): _*),
      Composite(tests.map(s => Composite(div(id = "label" + s.id, R(<h5>{s.id} <small>{s.name}{viewButton(s.id).render}</small></h5>)).styles(clear(both)))): _*),
      div().styles(marginBottom("10px")) //aka spacer
    ).render

  //TODO: show a link button which puts the permalink url in the clipboard ...
  private def viewButton(testId: String) = new ServerSideButton() {
    val id = "viewTest" + testId
    //TODO: this blows somewhat
    //TODO: theres a widget in here somewhere ... jetpac
    //TODO: could lose spacer and use marginLeft here
    //TODO: consider making the selected on go bold or something
    def presentation = ButtonPresentation(
        span(
          span().classes(glyphicon("chevron-right")).styles(marginLeft("5px"))
        )
      .render
    )

    def onClick = viewTest(testId)
  }

  private[agent] def viewTest(id: String) = {
    subscriber ! ViewTest(id)
    //this is a bit .. because I seems to get thrown away ...
    nothing
//    Element(id = "label" + id).addClass("selected")
    //    button.element.disable
    //TODO: this should proable disable and then do an ajaxInvoke to RunTest(x) ...
    //TODO: should also clear the activityAgent - which is why this will all be easier in an agent ...
  }
}